package com.coe3.data;

import java.util.UUID;

import com.coe3.android3coelibrary.R;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;
import android.view.Window;

public class Information {
	
	static public final String TAG_DBUG = "chud";
	
	static public final String OS = "android";
	
	static public final String STR_COMMA = ",";
	static public final String STR_EMPTY = "";
	
	/**
	 * 
	 * @param context
	 * @return get Email: SessionManager.KEY_EMAIL
	 * 		   get Password: SessionManager.KEY_PASSWORD
	 */
	public static String getEmailSession(Context context) {
		/**
         * Call this function whenever you want to check user login
         * This will redirect user to LoginActivity is he is not
         * logged in
         * */
	    if (SessionManager.isLoggedIn(context)) 
	    	// get user data from session
	        return SessionManager.getUserDetails(context).get(SessionManager.KEY_EMAIL);
		else
			return "";
	}
	
	public static String getEmail(Context context) {
	    AccountManager accountManager = AccountManager.get(context); 
	    Account account = getAccount(accountManager);
	
	    if (account == null) {
	      return null;
	    } else {
	      return account.name;
	    }
	}
	
	private static Account getAccount(AccountManager accountManager) {
		Account[] accounts = accountManager.getAccountsByType("com.google");
		Account account;
		if (accounts.length > 0) {
			account = accounts[0];      
		} else {
			account = null;
		}
		return account;
	}
	
	public static String getUA() {
		return Build.MODEL;
	}
	
	public static String getAppVersion(Context context) {
		try {
			return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static void setLastApplicationID(Context context, String id) {
		DataStorage.setData(context, DataStorage.LAST_APPLICATION_ID, id);
	}
	
	public static String getLastApplicationID(Context context) {
		return DataStorage.getData(context, DataStorage.LAST_APPLICATION_ID, "0");
	}
	
	public static void setMacAddress(Context context, String mac_addr) {
		DataStorage.setData(context, DataStorage.KEY_MACADDRESS, mac_addr);
	}
	
	public static String getMacAddress(Context context) {
		if(DataStorage.getData(context, DataStorage.KEY_MACADDRESS, null) == null)
			setMacAddress(context, ((WifiManager)context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getMacAddress());
		
		return DataStorage.getData(context, DataStorage.KEY_MACADDRESS, null);
	}
	
	public static void checkMacAddress(Context context, String mac_addr) {
		if(!getMacAddress(context).equals(mac_addr))
		{
			setMacAddress(context, mac_addr);
		}//end if
	}
	
	public static void setUUID(Context context, String uuid) {
		DataStorage.setData(context, DataStorage.KEY_UUID, uuid);
	}
	
	public static String getUUID(Context context) {
		
		if(DataStorage.getData(context, DataStorage.KEY_UUID, null) == null)
			setUUID(context, UUID.randomUUID().toString());
		
		return DataStorage.getData(context, DataStorage.KEY_UUID, null);
	}
	
	public static void checkUUID(Context context, String uuid) {
		if(!getUUID(context).equals(uuid))
		{
			setUUID(context, uuid);
		}//end if
	}
	
	public static void clearRateComic(Context context, String key) {
		DataStorage.setBoolData(context, key, false);
	}
	
	public static Boolean isRateComic(Context context, String key) {
		if (!DataStorage.getBoolData(context, key, false)){
			DataStorage.setBoolData(context, key, true);
			return false;
		} else {
			return true;
		}//end if
	}
	
	public static void setStartApp(Context context, Boolean isStart) {
		DataStorage.setBoolData(context, DataStorage.KEY_IS_OPENAPP, isStart);
	}
	
	public static Boolean getStartApp(Context context) {
		if(checkStartApp(context)){
			setStartApp(context, false);
			return true;
		} else {
			return false;
		}//end if
	}
	
	public static Boolean checkStartApp(Context context) {
		return DataStorage.getBoolData(context, DataStorage.KEY_IS_OPENAPP, true);
	}
	
	/**
	 * 
	 * @param context
	 * @param id
	 * @return true: is first read, false otherwise.
	 */
	public static Boolean checkFirstRead(Context context, String id) {
		if (!DataStorage.getBoolData(context, id, false)) 
			return DataStorage.setBoolData(context, id, true);
		else
			return false;
	}
	
	public static int getCurrentPage(Context context, String id) {
		int ret = DataStorage.getIntData(context, id, 0);
		if (ret == 0) {
			DataStorage.setIntData(context, id, 0);
			return 0;
		} else {
			return ret;
		}//end if
	}
	
	public static String getOS() {
		return Information.OS;
	}
	
	public static String ck(String pin) {
		char[] pins = pin.toCharArray();
		
		int sum = 0;
		for (char c : pins) {
			sum += Character.getNumericValue(c);
			Log.d(TAG_DBUG, "sum + " + Character.getNumericValue(c));
			Log.d(TAG_DBUG, "sum: " + sum);
		}//end for
		
		Log.d(TAG_DBUG, "#sum: " + (double)sum);
		Log.d(TAG_DBUG, "#div: " + (pins[pins.length - 1] == '0' ? (double)Character.getNumericValue('9') : (double)Character.getNumericValue(pins[pins.length - 1])));
		Log.d(TAG_DBUG, "#Math.floor: " + (int)Math.floor((double)sum / (pins[pins.length - 1] == '0' ? (double)Character.getNumericValue('9') : (double)Character.getNumericValue(pins[pins.length - 1]))));
		
		String ck = String.valueOf((int)Math.floor((double)sum / (pins[pins.length - 1] == '0' ? (double)Character.getNumericValue('9') : (double)Character.getNumericValue(pins[pins.length - 1]))));
		
		return ck;
	}
	
	/**
	 * Scale up to fit screen width.
	 * @param screen_width
	 * @param image_width
	 * @param image_height
	 * @return 
	 */
	public static int height_scale_up(int screen_width, int image_width, int image_height) {
		float scale = (float)screen_width / (float)image_width;
		return (int)(image_height * scale);
	}
	
	/**
	 * Scale down to fit screen width.
	 * @param screen_width
	 * @param image_width
	 * @param image_height
	 * @return 
	 */
	public static int height_scale_down(int screen_width, int image_width, int image_height) {
		float scale = (float)image_width / (float)screen_width;
		return (int)(image_height / scale);
	}
	
	/**
	 * Dialog.
	 */
	
	public static Dialog mInstanceDialog;
	
	public static Dialog getInstanceDialog(Activity main) {
		if(mInstanceDialog == null)
			mInstanceDialog = new Dialog(main, R.style.dialog_theme);
		
		return mInstanceDialog;
	}
	
	public static void showActivityDialog_notitle(Activity main, int layoutId) 
    {
    	try {
    		mInstanceDialog = new Dialog(main, R.style.dialog_theme);
    		mInstanceDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    		mInstanceDialog.setContentView(layoutId);
    		mInstanceDialog.setCancelable(false);
    		mInstanceDialog.show();
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
	
	public static void close_dialog() 
    {
    	try {
    		if(mInstanceDialog != null){
	    		mInstanceDialog.hide();
	    		mInstanceDialog.dismiss();
    		}//end if
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
	
	public static void logDEBUG(String msg) {
		Log.d(TAG_DBUG, msg);
	}
	
	/**
	 * @param str
	 * @return return false is null
	 */
	public static String setData(String str) {
		if(str != null && !str.trim().equals(STR_EMPTY))
			return str;
		else
			return null;
	}
	
	/**
	 * @param str
	 * @param defValue
	 * @return return false is defValue
	 */
	public static String setData(String str, String defValue) {
		if(str != null && !str.trim().equals(STR_EMPTY))
			return str;
		else
			return defValue;
	}
}
